//
//  VideoChatViewController.m
//  模拟-百思不得姐
//
//  Created by 程春玉 on 16/8/17.
//  Copyright © 2016年 colin. All rights reserved.
//

#import "VideoChatViewController.h"
#import <HyphenateFullSDK/EMSDKFull.h>

@interface VideoChatViewController ()
@property (strong, nonatomic, readonly) id<IEMCallManager> callManager;
@property (nonatomic, copy) NSString *sesson_id;
@end

@implementation VideoChatViewController

- (id<IEMCallManager>)callManager
{
    return [EMClient sharedClient].callManager;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self call];
    
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear: animated];
    [self createBackBtn];
}

- (void)createBackBtn
{
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    [btn setBackgroundImage:[UIImage imageNamed:@"Profile_AddV_authen"] forState:(UIControlStateNormal)];
    btn.frame = CGRectMake(0, 0, 40, 40);
    btn.x = self.view.center.x;
    btn.y = self.view.height - 70;
    [btn addTarget:self action:@selector(btnClick) forControlEvents:(UIControlEventTouchUpInside)];
    [self.view addSubview:btn];
    [self.view bringSubviewToFront:btn];
}

- (void)btnClick
{
    UIAlertController *al = [UIAlertController alertControllerWithTitle:nil message:@"确定关闭视频通话吗" preferredStyle:(UIAlertControllerStyleAlert)];
    UIAlertAction *sure = [UIAlertAction actionWithTitle:@"确定" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        [[EMClient sharedClient].callManager endCall:_sesson_id reason:(EMCallEndReasonHangup)];
        [self dismissViewControllerAnimated:NO completion:nil];
    }];
    UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:nil];
    [al addAction:sure];
    [al addAction:cancel];
    [self presentViewController:al animated:YES completion:nil];
}

// 发起视频通话要求
- (void)call
{
    EMError *error = nil;
    EMCallSession *callSession = [self.callManager makeVideoCall:_userName error:&error];
    self.sesson_id = callSession.sessionId;
    if (!error) {
         //1.对方窗口
        callSession.remoteView = [[EMCallRemoteView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
         [self.view addSubview:callSession.remoteView];
         //2.自己窗口
         CGFloat width = 80;
         CGFloat height = self.view.frame.size.height / self.view.frame.size.width * width;
         callSession.localView = [[EMCallLocalView alloc] initWithFrame:CGRectMake(0, 0, width, height)];
         [self.view addSubview:callSession.localView];
    } else {
        NSLog(@"%@", error);
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
